package hidalgo.expenses.dao;

import hidalgo.expenses.entities.Note;
import hidalgo.expenses.utils.Util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class NoteDao extends BaseDao {

	public static String NOTEID = "note_id";
	public static String LASTMODIFIEDDATE = "lastmodifieddate";
	public static String TEXT = "text";
	public static String USERID = "user_id";

	public static Note getNotesById(int noteId) {
		MyHttpClient conection = new MyHttpClient(URLSELECT);

		conection.addDataToSend("queryphp", "SELECT * FROM note WHERE "
				+ NOTEID + "='" + noteId + "' ");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				JSONArray resultados = jsonObjRecv.getJSONArray("resultados");

				if (resultados != null) {

					Note note = null;

					for (int i = 0; i < resultados.length(); i++) {

						JSONObject elemento = (JSONObject) resultados.get(i);

						note = new Note(elemento.getInt(NOTEID),
								Util.getDateFromString(
										elemento.getString(LASTMODIFIEDDATE),
										Util.SQL_DATE_FORMAT),
								elemento.getString(TEXT),
								elemento.getInt(USERID));
					}

					resultados = null;

					return note;
				} else {
					return null;
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	public static ArrayList<Note> getNotesByUser(int userId) {

		MyHttpClient conection = new MyHttpClient(URLSELECT);

		conection.addDataToSend("queryphp", "SELECT * FROM note WHERE "
				+ USERID + "='" + userId + "' ORDER BY lastmodifieddate DESC");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {
				JSONArray resultados = jsonObjRecv.getJSONArray("resultados");

				if (resultados != null) {

					ArrayList<Note> noteList = new ArrayList<Note>();

					for (int i = 0; i < resultados.length(); i++) {

						JSONObject elemento = (JSONObject) resultados.get(i);

						Note note = new Note(elemento.getInt(NOTEID),
								Util.getDateFromString(
										elemento.getString(LASTMODIFIEDDATE),
										Util.SQL_DATE_FORMAT),
								elemento.getString(TEXT),
								elemento.getInt(USERID));

						noteList.add(note);

					}

					resultados = null;

					return noteList;
				} else {
					return null;
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	public static boolean insert(Note note) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		conection.addDataToSend(
				"queryphp",
				"INSERT INTO note "
						+ "("
						+ TEXT
						+ ","
						+ USERID
						+ ","
						+ LASTMODIFIEDDATE
						+ ") "
						+ " VALUES('"
						+ note.getText()
						+ "','"
						+ note.getUserId()
						+ "','"
						+ Util.getStringFromDate(note.getLastModifiedDate(),
								Util.SQL_DATE_FORMAT) + "')");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {

				success = jsonObjRecv.getBoolean("resultados");

				return success;
			} catch (JSONException e) {
				e.printStackTrace();
				return success;
			}
		} else {
			return success;
		}

	}

	public static boolean update(Note note) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		String sql = " UPDATE note SET "
				+ TEXT + "='" + note.getText() + "',"
				+ LASTMODIFIEDDATE
				+ "='" + Util.getStringFromDate(note.getLastModifiedDate(),
						Util.SQL_DATE_FORMAT) + "' WHERE " + NOTEID + "= '"
				+ note.getNoteId()+"' ";

		conection.addDataToSend("queryphp", sql);

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {

				success = jsonObjRecv.getBoolean("resultados");

				return success;
			} catch (JSONException e) {
				e.printStackTrace();
				return success;
			}
		} else {
			return success;
		}

	}

	public static boolean deleteNoteById(int noteId) {

		Boolean success = false;

		MyHttpClient conection = new MyHttpClient(URLINSERT_UPDATE_DELETE);

		conection.addDataToSend("queryphp", "DELETE FROM note WHERE " + NOTEID
				+ "='" + noteId + "' ");

		JSONObject jsonObjRecv = conection.executeHttpPost();

		if (jsonObjRecv != null) {
			try {

				success = jsonObjRecv.getBoolean("resultados");

				return success;
			} catch (JSONException e) {
				e.printStackTrace();
				return success;
			}
		} else {
			return success;
		}
	}

}